#include "BaseToken.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"

BaseToken::BaseToken(void)
{
	// Assign the class' token ID
	m_nTokenID = Base_Token;

	m_fPosX = 0.0f;
	m_fPosY = 0.0f;
	m_fVelX = 0.0f;
	m_fVelY = 0.0f;

	m_nWidth = 0;
	m_nHeight = 0;

	m_nImageID = -1;

	m_unRefCount = 1;
}


BaseToken::~BaseToken(void)
{
}

void BaseToken::Update( float fElapsedTime )
{
	// Move the token by the velocity in pixels per second.
	m_fPosX += m_fVelX * fElapsedTime; 
	m_fPosY += m_fVelY * fElapsedTime; 
}

void BaseToken::Render( void )
{
	// Get the instance of the Texture Manager and draw the token.
	CSGD_TextureManager::GetInstance()->Draw( m_nImageID, int(m_fPosX), int(m_fPosY) );
}

RECT BaseToken::GetRect( void ) const
{
	// Create a Rectangle
	RECT rCollider = { };

	// Use the positions to get the left and top
	rCollider.left = LONG(m_fPosX);
	rCollider.top  = LONG(m_fPosY);

	// Now use the the positions PLUS the sizes for the right and bottom
	rCollider.right = LONG(m_fPosX + m_nWidth);
	rCollider.bottom = LONG(m_fPosY + m_nHeight);

	return rCollider;
}

void BaseToken::AddRef( void )
{
	// Increase the reference count.
	m_unRefCount++;
}

void BaseToken::Release( void )
{
	// Decrease the reference count.
	m_unRefCount--;

	// Check if there nothing referencing you...
	if( m_unRefCount == 0 )
		delete this; // If so, self destruct!
}

bool BaseToken::CheckCollision( IBaseToken* pOther )
{
	// Create an overlapping rect to take in the other token and your collision rects.
	RECT rOverlap = { };
	RECT rSelf = this->GetRect();
	RECT rOther = pOther->GetRect();

	// Check if the have collided
	if( IntersectRect( &rOverlap, &rSelf, &rOther ) == TRUE )
	{
		// If so return true
		return true;
	}

	// Return false if no collision happens
	return false;
}
